package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.pet.domain.PetType;
import cn.itsource.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private cn.itsource.pet.mapper.PetTypeMapper PetTypeMapper;

    @Override
    public List<PetType> queryTree() {
        List<PetType> allTypes = PetTypeMapper.findAll();
        Map<Long,PetType> allTypeMap = new HashMap<>();
        for (PetType dept : allTypes){
            allTypeMap.put(dept.getId(),dept);
        }
        List<PetType> petTypeTree = new ArrayList<>();
        for (PetType temp :allTypes){
            if(temp.getParentId()==null){
                petTypeTree.add(temp);
            }else{
                PetType parent = allTypeMap.get(temp.getParentId());
                if(parent!=null){
                    parent.getChildren().add(temp);
                }
            }
        }
        return petTypeTree;
    }
}
